#include <stdio.h>
#include <stdlib.h>

#include "array_sentinel.h"

struct set* set__empty(struct set* s)
{
  s->s[0] = '\0';
  s->size = 0;
  return s;
}

size_t set__size (const struct set * s )
{
  return s->size;
}

int set__is_empty (const struct set * s)
{
  return (!s->size);
}

struct set* set__union(const struct set * s1, const struct set * s2, struct set * su)
{
  int l1 = set__size (s1), l2 = set__size (s2), k=0;
  set__empty(su);
  for (k=0; k<l1 ; k++)
    {
      set__add( su, s1->s[k]);
    }
  for (k=0; k<l2 ; k++)
    {
      set__add( su, s2->s[k]);
    }
  return su;
}


struct set* set__add( struct set* s, char c)
{
  if (set__find(s ,c)  == 0)
    {
      if(set__size(s) >= SET__SIZE-1)
	return NULL;
      int l = set__size (s);
      s->s[l] = c;
      s->s[l+1] = '\0';
      s->size++;
    }
  return s;
}

struct set * set__remove (struct set * s, char c)
{
  int k =0 ;
  for (k=0 ; k<= s->size; k++)
    {
      if (s->s[k] == c)
	{
	  char dernier = s->s[s->size-1];
	  s-> s[k] = dernier;
	  s->s[s->size] = '\0';
	  s->size --;
	}
    }
  return s;
}

int  set__find(const struct set * s, char c)
{
  int k;
  int l = set__size(s);
  for (k=0; k< l;k++)
    {
      if (s->s[k] == c)
	return 1;
    }
  return 0;
}

struct set* set__inter(const struct set* s1, const struct set* s2, struct set* res)
{
  const struct set* small_set;// = (set__size(s1)<=set_size(s2))?s1:s2;
  const struct set* big_set;// = (set__size(s1)<=set_size(s2))?s2:s1;
  set__empty(res);
  if( set__size(s1) <= set__size(s2) ){
    small_set = s1;
    big_set = s2;
  }
  else{
    small_set = s2;
    big_set = s1;
  }
  int i;
  for(i=0; i<set__size(small_set) ;i++){
    if( set__find( big_set, small_set->s[i] ) ){
      set__add( res , small_set->s[i] );
    }
  }
  return res;
}

void set__display(struct set* s)
{
  int i;
  for(i=0; i <= s->size; i++)
  {
    printf("%c ",s->s[i]);
  }
}


int compare (struct set * s1 , struct set * s2)
{
  int k;
  int l1 = set__size (s1);
  int l2 = set__size (s2);
  if (l1==l2) 
    {
      for (k=0 ; k < l1; k++)
	{
	  if ( ! set__find(s2, s1->s[k]))
	    return (0);
	} 
      return (1);
    }
  else
    {
      return(0);
    }
  
}

struct set* set__copy (struct set * o, struct set * d)
{
  int k;
  d->size = o->size;
  for (k=0 ; k<=o->size; k++)
    {
      d->s[k] = o->s[k];
    }
  return d;
}











